import {GlobalDictList, setGlobalDictList} from "@/utils/static";
import localDict from "@/utils/localDict";
import {parse} from "querystring";
import fieldsLocal from "@/utils/fieldjson";
import indexStore from "@/pages/model"

//获取查询参数
export const getPageQuery = () => parse(window.location.href.split("?")[1]);

//==============合并字段相关====================
//定义合并的字段信息,用来处理特殊的字段需要合并显示
const mergeList = [
  ["mergeTime", "时间区间", "startTime", "endTime"],
  ["mergeArea", "行政区域", "proName", "cityName", "areaName"],
  ["mergeDay", "日期区间", "startDay", "endDay"],
];

//对两个字段进行合并,比如startTime,endTime,可以支持3个字段合并,合并后是个数组[,,]
export const getMergeFields = (fields: {}) => {
  let realFields = {};
  //console.log("fields",Object.values(fields));

  //获取符合合并的区段,如果没有一个符合,那么就直接返回
  let checkMerge = true;
  Object.keys(fields).map(item => {
    mergeList.map(m => {
      item == m[0] ? checkMerge = false : null;
    })
  });
  if (checkMerge == false) {
    return fields;
    return;
  }

  //判断是否是全部符合条件的字段才进行合并
  mergeList.map((m) => {
    //检测mergeList后面的字段是否都包含
    let tcheck = false;
    m.slice(2, 10).map(tItem => {
      const f1 = Object.keys(fields).findIndex((item) => item == tItem);
      if (f1 == -1) tcheck = true;
    });

    //当检测通过满足每条字段的存在通过的情况下,就对字段进行合并,产生新的字段
    if (tcheck == false) {
      m.slice(2, 10).map(tItem => {
        const f1 = Object.keys(fields).findIndex((item) => item == tItem);
        //在赋值的时候需要判断,这个字段是否在需要合并的字段里面,如果再就剔除出来
        //格式举例 mergeArea:["北京市", "市辖区", "朝阳区"]
        if (f1 != -1) {
          if (realFields[m[0]] == undefined) {
            realFields[m[0]] = []
          }
          realFields[m[0]].push(Object.values(fields)[f1])
        }
      });

      //对其他的值进行赋值排出掉多余的字段
      Object.keys(fields).map((item, index) => {
        //判断是否是在存在merge的合并字段出现的数据
        const f1 = m.slice(2, 10).findIndex(vitem => vitem == item);
        if (f1 == -1 && item !== "" && item !== undefined) {
          const tKey = Object.keys(fields)[index];
          realFields[tKey] = fields[tKey];
        }
      });

    }
  });

  //console.log("realFields",realFields);
  return Object.keys(realFields) == 0 ? fields : realFields;
};

//对字段合并之后进行分解赋值
export const getRestoreFields = (realFields: {}) => {
  let restoreFields = {...realFields};
  //获取合并的数组定义
  mergeList.map(m => {
    Object.keys(realFields).map((item, index) => {
      //console.log('realFields=>>>',item);
      if (item == m[0]) {
        const tValue = Object.values(realFields)[index];
        if (tValue) {
          restoreFields[m[2]] = tValue[0] ? tValue[0] : "";//数组的获取0
          restoreFields[m[3]] = tValue[1] ? tValue[1] : "";//数组的获取1
          //兼容三个字段的合并
          if (m.length > 4 && tValue.length > 2) {
            restoreFields[m[4]] = tValue[2] ? tValue[2] : "";//数组的获取
          }
        }
      }
    });
  });

  console.log(restoreFields, "restoreFields===");
  return restoreFields;
};
//==============合并字段相关====================


//==============获取真的使用字段====================
//获取可用的字段信息,对于空白的和未定义的与已排除
export const getRealFields = (fields: {}) => {
  let realFields = {};
  Object.values(fields).map((item, index) => {
    const tKey = Object.keys(fields)[index];
    if (item !== undefined) {
      realFields[tKey]=fields[tKey];
    }
    /*if (item !== "" && item !== undefined) {
      realFields[tKey]=fields[tKey];
    }*/
    /*if (item !== "" || item !== undefined) {
      realFields[tKey] = fields[tKey] === undefined ? "" : fields[tKey];
    }*/
  });
  //需要对合并的字段分开成单独的属性
  console.log(realFields, "realFieldsrealFields===");
  return getRestoreFields(realFields);
};

//强制显示的排序,按照字段设计的来显示
export const getSorted = (dataKey, data) => {
  let tdata = {};
  dataKey.map(item => {
    const findex = Object.keys(data).findIndex(v => v == item);
    if (findex != -1) {
      const key = Object.keys(data)[findex];
      const value = Object.values(data)[findex];
      tdata[key] = value;
    }
  })
  return tdata;
}
//==============获取真的使用字段====================



//==============数据字典相关====================
//在本地数据中获取
export const getDictListLocalDict = (code) => {
  const find = localDict.find((item) => item.code == code);
  if (find !== undefined) {
    return find.list.map((item) => ({
      name: item.field_name,
      value: item.value,
    }));
  } else {
    return undefined;
  }
};

//获取list的键值对
export const getDictList = (code) => {
  const find = GlobalDictList.find((item) => item.typeCode == code);
  if (find !== undefined) {
    return find.list.map((item) => ({
      name: item.typeLabel,
      value: item.typeValue,
    }));
  } else {
    //如果找不到数据字典就再本地字典里找下
    //return undefined;
    return getDictListLocalDict(code);
  }
};

//根据value, 获取字典对应name(字典)
export const getDictLabel = (typeCode, defaultValue) => {
  let list = []
  if(indexStore.dict.length){
    indexStore.dict.map((item) => {
      if(item.typeCode === typeCode){
        list = item.list
      }
    })
  }
  //没有找到匹配的组件字典
  if (!list || list.length == 0) {
    return defaultValue;
  }else{
    let obj = list.find((item) => item.typeValue === defaultValue)
    if(obj){
      return obj.typeLabel
    }else{
      return defaultValue
    }
  }
};


//获取控件对应的字典,设置好固定死的关系 ,组件与字典的关系
//公共的可以在这里进行设置
export const getControlDictType = (typeCode) => {
  if (["marriage"].findIndex((i) => i == typeCode) != -1) {
    return getDictList("code_marriage");
    return;
  }
  if (["education"].findIndex((i) => i == typeCode) != -1) {
    return getDictList("code_education");
    return;
  }
  if (["isRightHandedness"].findIndex((i) => i == typeCode) != -1) {
    return getDictList("code_is_right");
    return;
  }
  if (["operationType"].findIndex((i) => i == typeCode) != -1) {
    return getDictList("code_log_operation_type");
    return;
  }
  if (["logType"].findIndex((i) => i == typeCode) != -1) {
    return getDictList("code_log_type");
    return;
  }
  if (["sex"].findIndex((i) => i == typeCode) != -1) {
    return getDictList("code_sex");
    return;
  }
  if (["nation"].findIndex((i) => i == typeCode) != -1) {
    return getDictList("code_nation");
    return;
  }
  if (["tmsMode"].findIndex((i) => i == typeCode) != -1) {
    return getDictList("code_cure_model");
    return;
  }
  if (["marriage"].findIndex((i) => i == typeCode) != -1) {
    return getDictList("code_marriage");
    return;
  }
  return [];
};

//获取控件对应字典的值,用来显示默认名称,传入字典list,和默认值 dictList=[{name,value}]
export const getControlDictDefaultValue = (dictList, defaultValue) => {
  const find = dictList.find((item) => defaultValue == item.value);
  return find ? find.value : "";
};

//获取控件对应字典的值,用来显示默认名称,传入字典list,和默认值 dictList=[{name,value}]
export const getControlDictDefaultName = (dictList, defaultValue) => {
  const find = dictList.find((item) => defaultValue == item.value);
  return find ? find.name : "";
};

//根据字段,和对应的value直接获取字典中的名称,用来显示,如果没有匹配到就直接返回值
export const getDictName = (typeCode, defaultValue) => {
  const dictList = getControlDictType(typeCode);
  //没有找到匹配的组件字典
  if (!dictList || dictList.length == 0) {
    return defaultValue;
    return;
  }
  const find = dictList.find((item) => defaultValue == item.value);
  return find ? find.name : defaultValue;
};
//==============数据字典相关====================

//菜单的自动转化,对于ID字段不是ID的绑定
export const convertMenu = (data) => {
  const func = (data) => {
    data.map((i, index) => {
      data[index] = {...i, ...{key: i.id, id: i.id}}
      if (i.children) {
        if (i.children.length == 0) {
          data[index].children = null
        } else {
          func(i.children)
        }
      }
    })
  }
  func(data)
  return data.filter(x =>x!=undefined)
}


//添加到全局的字段说明中,方便调用
export const addMergeListToGFieldsInfo = () => {
  const mList = mergeList.map((item) => {
    return {
      desc: item[1],
      dataType: "",
      propertyName: item[0],
    };
  });
  const newList = [...GlobalDictList, ...mList];
  setGlobalDictList(newList)
};

export const getDataName = (dataKey=[]) => {
  let ls=[];
  dataKey.map(item=>{
    const vitem=fieldsLocal.find(v=>item==v.propertyName);
    if(vitem){
      ls.push(vitem.desc)
    }else{
      ls.push("无匹配字段"+item)
    }
  });
  return ls;
}
